package com.gdth.http.method;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.gdth.bank.BankApplication;
import com.gdth.bank.model.TAppTokenModel;
import com.gdth.http.entity.HttpTokenResult;
import com.gdth.http.func.HttpAllResultFunc;
import com.gdth.http.func.HttpGetImageFunc;
import com.gdth.http.func.HttpTokenResultFunc;
import com.gdth.http.interceptor.MarvelSigningInterceptor;
import com.gdth.http.listener.ServletListener;
import com.gdth.resource.FunctionOnOff;
import com.gdth.rxjava.subscriber.SubscriberUtil;

import android.os.Environment;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Observable;
import rx.Subscriber;

public class HttpMethodServlet {
    private static final int DEFAULT_TIMEOUT = 120;

    private Retrofit retrofit;
    private ServletListener mServletService;
    
    /**
     * 单例模式
     * @author arvin
     *
     */
    private static class SingletonHolder{
        private static final HttpMethodServlet INSTANCE = new HttpMethodServlet();
    }

    public static HttpMethodServlet getInstance(){
        return SingletonHolder.INSTANCE;
    }

    /**
     * 私有构造方法
     */
    private HttpMethodServlet() {
        retrofit = new Retrofit.Builder()
                .client(getOkHttpClient())
                .addConverterFactory(GsonConverterFactory.create())//gson
//                .addConverterFactory(SimpleXmlConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .baseUrl(BankApplication.mInstance.url)
                
                .build();
        
        mServletService = retrofit.create(ServletListener.class);
    }
    
    public void resetUrl(String url) {
    	retrofit = new Retrofit.Builder()
        .client(getOkHttpClient())
        .addConverterFactory(GsonConverterFactory.create())//gson
//        .addConverterFactory(SimpleXmlConverterFactory.create())
        .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
        .baseUrl(BankApplication.mInstance.url)
        .build();

    	mServletService = retrofit.create(ServletListener.class);
    }
    
    /**
     * 手动创建一个OkHttpClient并设置超时时间
     * @return
     */
    private OkHttpClient getOkHttpClient(){

    	MarvelSigningInterceptor mMarvelSigningInterceptor = new MarvelSigningInterceptor();
    	OkHttpClient client = new OkHttpClient.Builder()
    		.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
//    		.connectTimeout(1000, TimeUnit.MINUTES)
		    .readTimeout(1000, TimeUnit.MINUTES)
		    .writeTimeout(1000, TimeUnit.MINUTES)
    		.addInterceptor(mMarvelSigningInterceptor)
            .dns(new EngDNS())
    		.sslSocketFactory(httpsUtils.getSSLSocketFactory())
    		.hostnameVerifier(httpsUtils.getHostnameVerifier())
    		.build();
    	
    	return client;
    }
    
    
   

    /**
     * 
     * @param subscriber  由调用者传过来的观察者对象
     * @param start 起始位置
     * @param count 获取长度
     */
    public void getTopMovieJson(Subscriber<String> subscriber, String arg0){
        Observable<String> observable = mServletService.upLoadCjkhxx(arg0)
                .map(new HttpAllResultFunc<String>());

        SubscriberUtil.toSubscribe(observable, subscriber);
    }
    
    /**
     * 登录
     * @param user
     * @return
     */
    public Observable<String> login(String val){
    	//登录时带上本地公有密钥
    	String publicKey = TAppTokenModel.getPublicKey();
    	return mServletService.login(val, publicKey).map(new HttpAllResultFunc<String>());
    }

    public Observable<String> getjxgzhz(String zh) {
        return mServletService.getjxgzhz(zh).map(new HttpAllResultFunc<String>());
    }
    
    public Observable<String> getAppImage(){
    	return mServletService.getAppiamge().map(new HttpGetImageFunc<String>());
    }
    
    /**
     * 获取令牌以及公有密钥
     * @param username
     * @return
     */
    public Observable<HttpTokenResult> getTokenForLogin(String val){
    	if(FunctionOnOff.isOpenToken) {
    		return mServletService.getTokenForLogin(val).map(new HttpTokenResultFunc<HttpTokenResult>());
    	}else {
    		return mServletService.getTokenForLogin(val).map(new HttpTokenResultFunc<HttpTokenResult>());
    	}
    	
    }
    
    /**
     * 上传采集客户信息
     * @param content
     * @return  delBfrjInfo
     */
    public Observable<String> upLoadCjkhxx(String content){
    	return mServletService.upLoadCjkhxx(content).map(new HttpAllResultFunc<String>());
    }
    //  时间: 2020/4/21 3:16 PM ,作者: xjc, 作用: 网络删除拜访记录
    public Observable<String> delBfrjInfo(String content){
        return mServletService.delBfrjInfo(content).map(new HttpAllResultFunc<String>());
    }

    public Observable<String> getWl(String content){
    	return mServletService.getWl(content).map(new HttpAllResultFunc<String>());
    }
    
    public Observable<String> getAllCjkhxx(String content){
    	return mServletService.getAllCjkhxx(content).map(new HttpAllResultFunc<String>());
    }
    public Observable<String> gethddj(String content){
    	return mServletService.gethddj(content).map(new HttpAllResultFunc<String>());
    }
    public Observable<String> getBmList(String content){
    	return mServletService.getBmList(content).map(new HttpAllResultFunc<String>());
    }
    public Observable<String> saveqbList(String content){
    	return mServletService.saveqbList(content).map(new HttpAllResultFunc<String>());
    }
    public Observable<String> savegxqbList(String content){
    	return mServletService.savegxqbList(content).map(new HttpAllResultFunc<String>());
    }
    public Observable<String> getDbjjdjList(String content){
    	return mServletService.getDbjjdjList(content).map(new HttpAllResultFunc<String>());
    }
    
    public Observable<String> getDbjjdjJson(String content){
    	return mServletService.getDbjjdjJson(content).map(new HttpAllResultFunc<String>());
    }
    public Observable<String> getbjjdjList(String content){
    	return mServletService.getbjjdjList(content).map(new HttpAllResultFunc<String>());
    }
    public Observable<String> getbjjdjUpList(String content){
    	return mServletService.getbjjdjUpList(content).map(new HttpAllResultFunc<String>());
    }
    public Observable<String> getbjjdjOffList(String content){
    	return mServletService.getbjjdjOffList(content).map(new HttpAllResultFunc<String>());
    }
    public Observable<String> getDbjjdjUpList(String content){
    	return mServletService.getDbjjdjUpList(content).map(new HttpAllResultFunc<String>());
    }
    public Observable<String> getDbjjdjOffList(String content){
    	return mServletService.getDbjjdjOffList(content).map(new HttpAllResultFunc<String>());
    }
    public Observable<String> getGwList(String content){
    	return mServletService.getGwList(content).map(new HttpAllResultFunc<String>());
    }
    public Observable<String> getdjJson(String content){
    	return mServletService.getdjJson(content).map(new HttpAllResultFunc<String>());
    }
    public Observable<String> deletejson(String content){
    	return mServletService.deletejson(content).map(new HttpAllResultFunc<String>());
    }
    public Observable<String> notificationList(String content){
    	return mServletService.notificationList(content).map(new HttpAllResultFunc<String>());
    }
    
    public Observable<String> delNotificationList(String content){		// 删除的相关接口
    	return mServletService.delNotificationList(content).map(new HttpAllResultFunc<String>());
    }
    
    /**
     * 获取全部机构信息
     * @param content
     * @return
     */
    public Observable<String> getAllBm(String content){
    	return mServletService.getAllBm(content).map(new HttpAllResultFunc<String>());
    }
    
    public Observable<String> upLoadDzcpPos(String content){
    	return mServletService.upLoadDzcpPos(content).map(new HttpAllResultFunc<String>());
    }
    public Observable<String> serachDzcpkhxxpos(String content){
    	return mServletService.serachDzcpkhxxpos(content).map(new HttpAllResultFunc<String>());
    }
    
    public Observable<String> upLoadDzcpEtc(String content){
    	return mServletService.upLoadDzcpEtc(content).map(new HttpAllResultFunc<String>());
    }
    
    
    /**
     * 获取令牌以及公有密钥
     * @param username
     * @return
     */
    public Observable<String> getBbGx(String content){
    	return mServletService.getBbGx(content).map(new HttpAllResultFunc<String>());
    }
    
    
//    public Observable<String> getBbGx(String content){
//    	return mServletService.getBbGx(content).map(new HttpAllResultFunc<String>());
//    }
    public Observable<String> getDzcpEtcListData(String content){
    	return mServletService.getDzcpEtcListData(content).map(new HttpAllResultFunc<String>());
    }
    //  时间: 2020/4/21 4:44 PM ,作者: xjc, 作用: ETC 删除
    public Observable<String> getDzcpEtcDelData(String content){
        return mServletService.getDzcpEtcDelData(content).map(new HttpAllResultFunc<String>());
    }

    public Observable<String> serachXx(String content){
    	return mServletService.serachXx(content).map(new HttpAllResultFunc<String>());
    }
    public Observable<String> getDzcpEtcYcjListData(String content){
    	return mServletService.getDzcpEtcYcjListData(content).map(new HttpAllResultFunc<String>());
    }
    public Observable<String> getBdserach(String content){
    	return mServletService.getBdserach(content).map(new HttpAllResultFunc<String>());
    }
    public Observable<String> getBdYcjserach(String content){
    	return mServletService.getBdYcjserach(content).map(new HttpAllResultFunc<String>());
    }
    public Observable<String> getDzcpEtcUpListData(String content){
    	return mServletService.getDzcpEtcUpListData(content).map(new HttpAllResultFunc<String>());
    }
    public Observable<String> getDzcpEtcYcjUpListData(String content){
    	return mServletService.getDzcpEtcYcjUpListData(content).map(new HttpAllResultFunc<String>());
    }
    public Observable<String> getWcjPosListData(String content){
    	return mServletService.getWcjPosListData(content).map(new HttpAllResultFunc<String>());
    }
    public Observable<String> getPosGeRenListData(String content){
    	return mServletService.getPosGeRenListData(content).map(new HttpAllResultFunc<String>());
    }
    public Observable<String> getSearchPos(String content){
    	return mServletService.getSearchPos(content).map(new HttpAllResultFunc<String>());
    }
    public Observable<String> getDzcpPosUpListData(String content){
    	return mServletService.getDzcpPosUpListData(content).map(new HttpAllResultFunc<String>());
    }
    public Observable<String> getDeleteDzcpPos(String content){
    	return mServletService.getDeleteDzcpPos(content).map(new HttpAllResultFunc<String>());
    }
    public Observable<String> getUpDzcpPosGeRenJl(String content){
    	return mServletService.getUpDzcpPosGeRenJl(content).map(new HttpAllResultFunc<String>());
    }
    public Observable<String> getOffDzcpPosGeRenJl(String content){
    	return mServletService.getOffDzcpPosGeRenJl(content).map(new HttpAllResultFunc<String>());
    }
    public Observable<String> getSearchDzcpPosGeRenJl(String content){
    	return mServletService.getSearchDzcpPosGeRenJl(content).map(new HttpAllResultFunc<String>());
    }
    public Observable<String> comparePosZjh(String content){
    	return mServletService.comparePosZjh(content).map(new HttpAllResultFunc<String>());
    }
    public Observable<String> searchDzcpPos(String content){
    	return mServletService.searchDzcpPos(content).map(new HttpAllResultFunc<String>());
    }
    public Observable<String> searchShaiXuanDzcpPos(String content){
    	return mServletService.searchShaiXuanDzcpPos(content).map(new HttpAllResultFunc<String>());
    }
    public Observable<String> getDzcpPosOfListData(String content){
    	return mServletService.getDzcpPosOfListData(content).map(new HttpAllResultFunc<String>());
    }
    /********************主动营销***********************/
    
    public Observable<String> saveZdyxsb(String content) {
    	return mServletService.saveZdyxsb(content).map(new HttpAllResultFunc<String>());
    }
    public Observable<String> findZdys(String content) {
    	return mServletService.findZdys(content).map(new HttpAllResultFunc<String>());
    }
    public Observable<String> updataSBBL(String content) {
    	return mServletService.updataSBBL(content).map(new HttpAllResultFunc<String>());
    }
    public Observable<String> pageZdyxsbList(String content) {
    	return mServletService.pageZdyxsbList(content).map(new HttpAllResultFunc<String>());
    }
    
    /********************存款维护***********************/
    
    public Observable<String> pageCkwhList(String content) {
    	return mServletService.pageCkwhList(content).map(new HttpAllResultFunc<String>());
    }
    
    public Observable<String> findCkwh(String content) {
    	return mServletService.findCkwh(content).map(new HttpAllResultFunc<String>());
    }
    /********************存款拓展***********************/
    public Observable<String> pageCktzList(String content) {
    	return mServletService.pageCktzList(content).map(new HttpAllResultFunc<String>());
    }
    
    public Observable<String> findCktz(String content) {
    	return mServletService.findCktz(content).map(new HttpAllResultFunc<String>());
    }
    /********************贷款管护***********************/
    public Observable<String> pageDkghList(String content) {
    	return mServletService.pageDkghList(content).map(new HttpAllResultFunc<String>());
    }
    
    public Observable<String> findDkgh(String content) {
    	return mServletService.findDkgh(content).map(new HttpAllResultFunc<String>());
    }
    
    public Observable<String> pageDkghAcList(String content) {
    	return mServletService.pageDkghAcList(content).map(new HttpAllResultFunc<String>());
    }
    
	public Observable<String> pageDkbsList(String content) {
		return mServletService.pageDkbsList(content).map(new HttpAllResultFunc<String>());
	}

    public Observable<String> findDkbsdj(String content) {
    	return mServletService.findDkbsdj(content).map(new HttpAllResultFunc<String>());
    }
	
    public Observable<String> pageDkbsdjAcList(String content) {
    	return mServletService.pageDkbsdjAcList(content).map(new HttpAllResultFunc<String>());
    }
    
    /*********************绩效考核**********************/
    public Observable<String> getCkkhjjhz(String content) {
    	return mServletService.getCkkhjjhz(content).map(new HttpAllResultFunc<String>());
    }
    public Observable<String> getJjJsHz(String content) {
    	return mServletService.getJjJsHz(content).map(new HttpAllResultFunc<String>());
    }
    public Observable<String> getJxgzhzmx(String content) {
    	return mServletService.getJxgzhzmx(content).map(new HttpAllResultFunc<String>());
    }
    public Observable<String> getDateByZH(String content) {
    	return mServletService.getDateByZH(content).map(new HttpAllResultFunc<String>());
    }
    /********************员工绩效***********************/
    public Observable<String> listBmByYhZh(String content) {
    	return mServletService.listBmByYhZh(content).map(new HttpAllResultFunc<String>());
    }
    public Observable<String> listYgByJg(String content) {
    	return mServletService.listYgByJg(content).map(new HttpAllResultFunc<String>());
    }
    /********************员工绩效***********************/
    public Observable<String> getYwljjhz(String content) {
    	return mServletService.getYwljjhz(content).map(new HttpAllResultFunc<String>());
    }
    public Observable<String> listYwljjjs(String content) {
    	return mServletService.listYwljjjs(content).map(new HttpAllResultFunc<String>());
    }

    /********************利息详情***********************/
    public Observable<String> getTjDate(String content) {
    	return mServletService.getTjDate(content).map(new HttpAllResultFunc<String>());
    }
    
    public Observable<String> pageLxList(String content) {
    	return mServletService.pageLxList(content).map(new HttpAllResultFunc<String>());
    }
    /*********************贷款营销汇总**********************/
    public Observable<String> getDkyxjjhz(String content) {
    	return mServletService.getDkyxjjhz(content).map(new HttpAllResultFunc<String>());
    }
    public Observable<String> listDkyxjjjs(String content) {
    	return mServletService.listDkyxjjjs(content).map(new HttpAllResultFunc<String>());
    }
    /*********************贷款营销汇总**********************/
    public Observable<String> getDkghjjhz(String content) {
    	return mServletService.getDkghjjhz(content).map(new HttpAllResultFunc<String>());
    }
    public Observable<String> getDkghJjJsHz(String content) {
    	return mServletService.getDkghJjJsHz(content).map(new HttpAllResultFunc<String>());
    }
    /*********************包收年度考核**********************/
    public Observable<String> getDkbsndkh(String content) {
    	return mServletService.getDkbsndkh(content).map(new HttpAllResultFunc<String>());
    }
    public Observable<String> listDkbsjjmx(String content) {
    	return mServletService.listDkbsjjmx(content).map(new HttpAllResultFunc<String>());
    }
    public Observable<String> getDkbsyqkhhz(String content) {
    	return mServletService.getDkbsyqkhhz(content).map(new HttpAllResultFunc<String>());
    }
    public Observable<String> listDkbsyqkhhzjjs(String content) {
    	return mServletService.listDkbsyqkhhzjjs(content).map(new HttpAllResultFunc<String>());
    }
    /*********************包收延期兑付**********************/
    public Observable<String> getDkbsyqdfhz(String content) {
    	return mServletService.getDkbsyqdfhz(content).map(new HttpAllResultFunc<String>());
    }
    public Observable<String> listDkbsyqdfMx(String content) {
    	return mServletService.listDkbsyqdfMx(content).map(new HttpAllResultFunc<String>());
    }
    /*********************管理绩效工资计价汇总**********************/
    public Observable<String> getGljxgzjjhz(String content) {
    	return mServletService.getGljxgzjjhz(content).map(new HttpAllResultFunc<String>());
    }
    public Observable<String> getBsyqdk(String content) {
    	return mServletService.getBsyqdk(content).map(new HttpAllResultFunc<String>());
    }
    public Observable<String> getYqdkbsnddqjs(String content) {
    	return mServletService.getYqdkbsnddqjs(content).map(new HttpAllResultFunc<String>());
    }
    public Observable<String> getYqdkbsnddqjsjs(String content) {
    	return mServletService.getYqdkbsnddqjsjs(content).map(new HttpAllResultFunc<String>());
    }
    public Observable<String> getGljxgzjjhzMx(String content) {
    	return mServletService.getGljxgzjjhzMx(content).map(new HttpAllResultFunc<String>());
    }
    public Observable<String> dzyhjjkhgzhz(String content) {
    	return mServletService.dzyhjjkhgzhz(content).map(new HttpAllResultFunc<String>());
    }
    public Observable<String> listDzyhjjjsMx(String content) {
    	return mServletService.listDzyhjjjsMx(content).map(new HttpAllResultFunc<String>());
    }
    public Observable<String> gljxgzjjhzMx(String content) {
    	return mServletService.gljxgzjjhzMx(content).map(new HttpAllResultFunc<String>());
    }
    public Observable<String> khkkmx(String content) {
    	return mServletService.khkkmx(content).map(new HttpAllResultFunc<String>());
    }
    public Observable<String> yfjxgzmx(String content) {
    	return mServletService.yfjxgzmx(content).map(new HttpAllResultFunc<String>());
    }
    public Observable<String> sgjjmx(String content) {
    	return mServletService.sgjjmx(content).map(new HttpAllResultFunc<String>());
    }
    public Observable<String> saveSbkcj(String content) {
    	return mServletService.saveSbkcj(content).map(new HttpAllResultFunc<String>());
    }
    public Observable<String> pageSbkcjxxList(String content) {
    	return mServletService.pageSbkcjxxList(content).map(new HttpAllResultFunc<String>());
    }
    public Observable<String> saveHdtz(String content) {
    	return mServletService.saveHdtz(content).map(new HttpAllResultFunc<String>());
    }
    public Observable<String> hdtzList(String content) {
    	return mServletService.hdtzList(content).map(new HttpAllResultFunc<String>());
    }
    public Observable<String> saveQxtz(String content) {
    	return mServletService.saveQxtz(content).map(new HttpAllResultFunc<String>());
    }
    public Observable<String> getHdtz(String content) {
    	return mServletService.getHdtz(content).map(new HttpAllResultFunc<String>());
    }
    public Observable<String> getHdmc(String content) {
    	return mServletService.getHdmc(content).map(new HttpAllResultFunc<String>());
    }
    
    //url访问，新接口
    public Observable<String> loadUrl(String url,String content) {
    	return mServletService.loadUrl(url,content).map(new HttpAllResultFunc<String>());
    }
    
    /********************数据同步***********************/
    public Observable<String> getCanshu() {
    	return mServletService.getCanshu("content").map(new HttpAllResultFunc<String>());
    }




    public Observable<String> getYhForUpdate(String content) {
    	return mServletService.getYhForUpdate(content).map(new HttpAllResultFunc<String>());
    }
    
    public Observable<String> uploadFile() {
    	File file = new File(Environment.getExternalStorageDirectory(), "icon.png");
    	RequestBody photoRequestBody = RequestBody.create(MediaType.parse("image/png"), file);
    	MultipartBody.Part photo = MultipartBody.Part.createFormData("photos", "icon.png", photoRequestBody);
    	
//    	return mServletService.uploadFiles(photo, RequestBody.create(null, "abc"), RequestBody.create(null, "123"));
    	return mServletService.uploadFile(RequestBody.create(null, "abc"), RequestBody.create(null, "123")).map(new HttpAllResultFunc<String>());
    }
    
    public Observable<String> uploadFiles() {
    	File file = new File(Environment.getExternalStorageDirectory(), "messenger_01.png");
        RequestBody photo = RequestBody.create(MediaType.parse("image/png"), file);
        
        Map<String,RequestBody> photos = new HashMap<String,RequestBody>();
        photos.put("photos\"; filename=\"icon.png", photo);
        photos.put("username",  RequestBody.create(null, "abc"));
        
        return mServletService.uploadFiles(photos, RequestBody.create(null, "123")).map(new HttpAllResultFunc<String>());
    	
    }
    //
    public Observable<String> saveTjSyJyb(String content){
    	return mServletService.saveTjSyJyb(content).map(new HttpAllResultFunc<String>());
    }
    
    public Observable<String> changepassword(String content){
    	return mServletService.changePassw(content).map(new HttpAllResultFunc<String>());
    }

    //  时间: 2020/5/11 5:28 PM ,作者: xjc, 作用: 获取所有登陆设备
    public Observable<String> getAppDevlist(){
        return mServletService.getAppDevlist("applist").map(new HttpAllResultFunc<String>());
    }

    //  时间: 2020/5/11 5:28 PM ,作者: xjc, 作用: 修改表格状态
    public Observable<String> delAppDev(String Content){
        return mServletService.delAppDev(Content).map(new HttpAllResultFunc<String>());
    }


}
